package com.caojing.syby.ui.brandsale.brandchildfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.caojing.syby.R;
import com.caojing.syby.base.BaseFragment;
import com.caojing.syby.bean.brand.BrandBean;
import com.caojing.syby.bean.brand.BrandRowsBean;
import com.caojing.syby.config.UrlConfig;
import com.caojing.syby.ui.adapter.BrandChildAdapter;
import com.caojing.syby.ui.brandsale.BrandsaleFragment;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;

/**
 * A simple {@link Fragment} subclass.
 */
public class BrandChildFragment extends BaseFragment implements BrandChildContract.View {

    @Bind(R.id.iv_Not_data)
    ImageView ivNotData;
    @Bind(R.id.lv_brand)
    PullToRefreshListView lvBrand;

    private BrandChildContract.Presenter presenter;
    private BrandChildContract.Model model;
    private int BRANDPAGE=1;
//    private int BCLASS=99;
    private List<BrandRowsBean> rows;
    private List<BrandRowsBean> totallist ;
    private ListView listView;
    private BrandChildAdapter brandChildAdapter;

    public static BrandChildFragment newInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString("bclass", argument);
        BrandChildFragment brandChildFragment = new BrandChildFragment();
        brandChildFragment.setArguments(bundle);
        return brandChildFragment;
    }
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_brand_list;

    }

    @Override
    protected void initView(View view) {

        presenter=new BrandChildPresenter(this);
        presenter.initBrandData(initParams());
    }

    private HashMap<String, String> initParams() {

        Bundle bundle=getArguments();
        String bclass = bundle.getString("bclass");
        HashMap<String,String> map=new HashMap<>();
        if (bclass!=null&&!"".equals(bclass)){
            map.put(UrlConfig.Key.ACT,UrlConfig.DefaultValue.ACTNEW);
            map.put(UrlConfig.Key.CPAGE,BRANDPAGE+"");
            map.put(UrlConfig.Key.PAGESIZE,UrlConfig.DefaultValue.PAGESIZE);
            map.put(UrlConfig.Key.BCLASS,bclass);
            map.put(UrlConfig.Key.V,UrlConfig.DefaultValue.V);
            return map;
        }

       return null;

    }

    @Override
    protected void initData() {


        //设置上啦刷新，下拉加载模式
        lvBrand.setMode(PullToRefreshBase.Mode.BOTH);
        lvBrand.setScrollbarFadingEnabled(true);
        //得到listView控件
        listView = lvBrand.getRefreshableView();

        lvBrand.setEmptyView(ivNotData);
        totallist = new ArrayList<>();
        brandChildAdapter = new BrandChildAdapter(getActivity(),totallist);
        listView.setAdapter(brandChildAdapter);

        lvBrand.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                lvBrand.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                BRANDPAGE++;
                presenter.initBrandData(initParams());

            }
        });

    }



    @Override
    public void onBrandDataSuccess(BrandBean brandBean) {
        rows = brandBean.getRows();
        totallist.addAll(rows);
        brandChildAdapter.notifyDataSetChanged();
        lvBrand.onRefreshComplete();
    }

    @Override
    public void onBrandDataFail(String msg) {
        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
    }
}
